<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.com_id"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          src="https://img.yzcdn.cn/vant/cat.jpeg"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate | relative }}</span>
            <van-button size="mini" type="default" @click="clickShowReply(item)" >回复{{ item.reply_count}}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" name="like-o" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model="value"
      >
        <van-button slot="button" size="mini" type="info" @click="hhhhh">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <!-- 弹出层 -->
      <van-popup v-model="isShowReply" round position="bottom" :style="{ height: '85%' }">
            <comment-reply  v-if="isShowReply" :article-id="id"  @close="isShowReply =false" :comment="currentComment"></comment-reply>
      </van-popup>
  </div>
</template>

<script>
import { reqAddComment } from '@/api/comment.js'
import { reqGetComments } from '@/api/search.js'
import commentReply from './commentReply.vue'
export default {
  name: 'ArticleComment',
  props: {
    id: {
      type: [String, Number],
      required: true
    }
  },
  components: {
    commentReply
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      value: '',
      isShowReply: false,
      currentComment: {}, // 表示当前的回复组件, 回复的是哪个评论
      articleId: ''
    }
  },

  methods: {
    // 获取评论数据
    async onLoad () {
      const { data: { data } } = await reqGetComments(this.id, this.offset)
      // 追加赋值给list
      const { results } = data

      this.list.push(...results)

      // 将last_id赋值 保证  下一页的数据的请求
      this.offset = data.last_id
      // 手动
      this.loading = false
      // 没有数据处理
      if (!results.length) {
        this.finished = true
      }
    },
    // 获取输入内容 发送请求
    async hhhhh () {
      // console.log(this.value)
      if (!this.value.trim()) return this.$toast('请输入评论')
      try {
        const { data: { data } } = await reqAddComment(this.id, this.value)
        // console.log(data.new_obj, 222)
        this.list.unshift(data.new_obj)
        this.value = ''
        this.$toast('评论成功')
      } catch (error) {
        this.$toast.error('禁止评论')
      }
    },
    // 回复弹框
    clickShowReply (item) {
      this.isShowReply = true
      // 2. 更新正在回复的组件
      this.currentComment = item
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
